package other.result;

import lombok.Data;

/**
 * 响应体
 */
@Data
public class R<T> {

    private int code;
    private String message;
    private T data;

    private R(int code, String message) {
        this.code = code;
        this.message = message;
    }

    private R(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T> R<T> ok(T data) {
        return new R<>(Code.SUCCESS.getCode(), Code.SUCCESS.getMsg(), data);
    }

    public static <T> R<T> ok() {
        return new R<>(Code.SUCCESS.getCode(), Code.SUCCESS.getMsg());
    }

    public static <T> R<T> fail() {
        return new R<>(Code.FALL.getCode(), Code.FALL.getMsg());
    }

    public static <T> R<T> fail(String msg) {
        return new R<>(Code.FALL.getCode(), msg);
    }

    public static <T> R<T> fail(Code code) {
        return new R<>(code.getCode(), code.getMsg());
    }

    public static <T> R<T> fail(Throwable e) {
        return new R<>(Code.FALL.getCode(), e.getMessage());
    }
}
